package com.cosmic.pizza.dao.Impl;

import com.cosmic.pizza.dao.OrderDao;
import com.cosmic.pizza.pojo.Order;
import com.cosmic.pizza.pojo.OrderInfo;
import com.cosmic.pizza.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    //查询用户的所有订单
    public List<Order> findAllOrder(int uid, int pageSize, int start) {
        String sql = "select * from tab_total_order where uid = ? limit ?,?";
        try {
            List<Order> orders = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Order>(Order.class),uid,start,pageSize);
            return orders;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    //查询订单的详细信息，返回OrderInfo的List，并在service层封装进Order
    public List<OrderInfo> findOrderInfoByOid(String oid) {
        String sql = "select * from tab_order_detail where oid = ?";
        List<OrderInfo> orderInfos = jdbcTemplate.query(sql,new BeanPropertyRowMapper<OrderInfo>(OrderInfo.class),oid);
        return orderInfos;
    }

    @Override
    public void insertCartOrder(Order order) {
        String sql = "insert into tab_total_order" +
                "(oid, uid, date, delivery, payFlag, getFlag, total_price) " +
                "values(?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,order.getOid(),order.getUid(),new Date(),order.getDelivery(),order.getPayFlag(),order.getGetFlag(),order.getTotal_price());
    }

    @Override
    public void insertOrderInfos(OrderInfo orderInfo) {
        String sql = "insert into tab_order_detail" +
                "(oid, fid, size, sum)" +
                "values (?,?,?,?)";
        jdbcTemplate.update(sql,orderInfo.getOid(),orderInfo.getFid(),orderInfo.getSize(),orderInfo.getSum());
    }

    @Override
    public void checkOutOrder(String oid, String address, String name) {
        String sql = "update tab_total_order set address = ? , name = ? where oid = ?";
        jdbcTemplate.update(sql,address,name,oid);
    }

    @Override
    public int PayOrder(String oid) {
        String sql = "update tab_total_order set payFlag = 'Y' where oid = ?";
        return jdbcTemplate.update(sql,oid);
    }

    @Override
    public int GetFlagOrder(String oid) {
        String sql = "update tab_total_order set getFlag = 'Y' where oid = ?";
        return jdbcTemplate.update(sql,oid);
    }

    @Override
    public List<Order> findNoneGetOrder(int uid) {
        String sql = "select * from tab_total_order where uid = ? and getFlag = 'N'";
        try {
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Order>(Order.class),uid);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<Order> findNonePayOrder(int uid) {
        String sql = "select * from tab_total_order where uid = ? and payFlag = 'N'";
        try {
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Order>(Order.class),uid);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public int countByUid(int uid) {
        String sql = "select count(*) from tab_total_order where uid = ? ";
        return jdbcTemplate.queryForObject(sql,Integer.class,uid);
    }

    @Override
    public Order findOrderByOid(String oid) {
        String sql = "select * from tab_total_order where oid = ?";
        try {
            List<Order> orders = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Order>(Order.class),oid);
            return orders.get(0);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void eval(String oid, int fid, String size, String evalMes) {
        String sql = "update tab_order_detail set evalFlag = 'Y',eval = ? where oid = ? and fid = ? and size = ?";
        jdbcTemplate.update(sql,evalMes,oid,fid,size);
    }

    @Override
    public OrderInfo  findOrderInfo(String oid, int parseInt, String size) {
        String sql = "select * from tab_order_detail where fid = ? and size = ? and oid = ?";
        List<OrderInfo> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(OrderInfo.class), parseInt, size, oid);
        return query.get(0) ;
    }
}
